package com.lingjtx.iot.network.handle.service;

import com.alibaba.fastjson2.JSONObject;
import com.lingjtx.common.core.RedisOptions;
import com.lingjtx.common.core.constant.RedisConfig;
import com.lingjtx.common.core.handle.Injector;
import com.lingjtx.common.core.model.iot.DeviceBaseInfo;
import com.lingjtx.common.influx.client.InfluxQueryExecutor;
import com.lingjtx.iot.network.db.DeviceTemplate;
import com.lingjtx.iot.network.handle.MqttDataHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service("tslService")
@RequiredArgsConstructor
public class TslService implements MqttDataHandler<DeviceTemplate> {

    private final InfluxQueryExecutor executor;

    @Override
    public DeviceTemplate convertData(String productId, JSONObject json) {
        return null;
    }

    @Override
    public void insertToInfluxDB(DeviceBaseInfo device, DeviceTemplate deviceTemplate) {
        deviceTemplate.setCompanyId(device.getCompanyId());
        deviceTemplate.setDeviceId(device.getDeviceId());
        executor.write(deviceTemplate);
    }

    @Override
    public void updateRedis(DeviceBaseInfo device, DeviceTemplate deviceTemplate, RedisOptions redis) {
        redis.hmPut(RedisConfig.HASHKEY_DEVICE_INFO_IOT_REPORT + device.getCompanyId(), device.getDeviceId(), deviceTemplate);
        redis.expire(RedisConfig.HASHKEY_DEVICE_INFO_IOT_REPORT + device.getCompanyId(), RedisConfig.TIMEOUT_3_DAY);
    }

    @Override
    public void handleDeviceFault(DeviceBaseInfo device, DeviceTemplate deviceTemplate, RedisOptions redis, Injector dao) {

    }

    @Override
    public void refreshData(DeviceBaseInfo device, DeviceTemplate deviceTemplate) {

    }
}
